home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 4 / MacFormat n. 4 (Spain) / MacFormat 4.bin / La ciudad del ShareWare / Desarrollo / OutOfPhase1.1 Source / OutOfPhase Folder / InteractionWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-28  |  13.4 KB  |  443 lines

  1. /* InteractionWindow.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "InteractionWindow.h"
  31. #include "MainWindowStuff.h"
  32. #include "WindowDispatcher.h"
  33. #include "TextEdit.h"
  34. #include "Memory.h"
  35. #include "GlobalWindowMenuList.h"
  36. #include "Main.h"
  37. #include "GrowIcon.h"
  38. #include "DataMunging.h"
  39. #include "FindDialog.h"
  40.  
  41.  
  42. #define HEIGHT (100)
  43. #define INDENT (20)
  44. #define BOTTOM (5)
  45.  
  46.  
  47. struct InteractionWindowRec
  48.     {
  49.         MainWindowRec*            MainWindow;
  50.         WinType*                        ScreenID;
  51.         GenericWindowRec*        MyGenericWindow; /* how the window event dispatcher knows us */
  52.         MenuItemType*                MyMenuItem;
  53.         TextEditRec*                Editor;
  54.     };
  55.  
  56.  
  57. /* create a new interaction window.  the caller is responsible for registering the */
  58. /* interaction window with the main window. */
  59. InteractionWindowRec*    NewInteractionWindow(struct MainWindowRec* MainWindow)
  60.     {
  61.         InteractionWindowRec*        Window;
  62.  
  63.         CheckPtrExistence(MainWindow);
  64.         Window = (InteractionWindowRec*)AllocPtrCanFail(sizeof(InteractionWindowRec),
  65.             "InteractionWindowRec");
  66.         if (Window == NIL)
  67.             {
  68.              FailurePoint1:
  69.                 return NIL;
  70.             }
  71.         Window->MainWindow = MainWindow;
  72.         Window->ScreenID = MakeNewWindow(eDocumentWindow,eWindowClosable,
  73.             eWindowZoomable,eWindowResizable,INDENT,GetScreenHeight()
  74.             - WindowOtherEdgeWidths(eDocumentWindow) - HEIGHT - BOTTOM,
  75.             GetScreenWidth() - (2 + WindowOtherEdgeWidths(eDocumentWindow)) - 2 * INDENT,
  76.             HEIGHT,(void (*)(void*))&InteractionWindowUpdator,Window);
  77.         if (Window->ScreenID == 0)
  78.             {
  79.              FailurePoint2:
  80.                 ReleasePtr((char*)Window);
  81.                 goto FailurePoint1;
  82.             }
  83.         SetWindowName(Window->ScreenID,"Output");
  84.         Window->Editor = NewTextEdit(Window->ScreenID,
  85.             (TEScrollType)(eTEVScrollBar | eTEHScrollBar),GetMonospacedFont(),9,
  86.             -1,-1,GetWindowWidth(Window->ScreenID) + 2,GetWindowHeight(Window->ScreenID) + 2);
  87.         if (Window->Editor == NIL)
  88.             {
  89.              FailurePoint3:
  90.                 KillWindow(Window->ScreenID);
  91.                 goto FailurePoint2;
  92.             }
  93.         Window->MyGenericWindow = CheckInNewWindow(Window->ScreenID,Window,
  94.             (void (*)(void*,MyBoolean,OrdType,OrdType,ModifierFlags))&InteractionWindowDoIdle,
  95.             (void (*)(void*))&InteractionWindowBecomeActive,
  96.             (void (*)(void*))&InteractionWindowBecomeInactive,
  97.             (void (*)(void*))&InteractionWindowJustResized,
  98.             (void (*)(OrdType,OrdType,ModifierFlags,void*))&InteractionWindowDoMouseDown,
  99.             (void (*)(unsigned char,ModifierFlags,void*))&InteractionWindowDoKeyDown,
  100.             (void (*)(void*))&InteractionWindowClose,
  101.             (void (*)(void*))&InteractionWindowMenuSetup,
  102.             (void (*)(void*,MenuItemType*))&InteractionWindowDoMenuCommand);
  103.         if (Window->MyGenericWindow == NIL)
  104.             {
  105.              FailurePoint4:
  106.                 DisposeTextEdit(Window->Editor);
  107.                 goto FailurePoint3;
  108.             }
  109.         Window->MyMenuItem = MakeNewMenuItem(mmWindowMenu,"Output",0);
  110.         if (Window->MyMenuItem == NIL)
  111.             {
  112.              FailurePoint5:
  113.                 CheckOutDyingWindow(Window->MyGenericWindow);
  114.                 goto FailurePoint4;
  115.             }
  116.         if (!RegisterWindowMenuItem(Window->MyMenuItem,(void (*)(void*))&ActivateThisWindow,
  117.             Window->ScreenID))
  118.             {
  119.              FailurePoint6:
  120.                 KillMenuItem(Window->MyMenuItem);
  121.                 goto FailurePoint5;
  122.             }
  123.         SetTextEditAutoIndent(Window->Editor,True);
  124.         SetTextEditTabSize(Window->Editor,MainWindowGetTabSize(MainWindow));
  125.         return Window;
  126.     }
  127.  
  128.  
  129. /* dispose of a interaction window.  the interaction window notifies the main window */
  130. /* that owns it. */
  131. void                                    DisposeInteractionWindow(InteractionWindowRec* Window)
  132.     {
  133.         CheckPtrExistence(Window);
  134.         MainWindowInteractionClosingNotify(Window->MainWindow,Window);
  135.         DeregisterWindowMenuItem(Window->MyMenuItem);
  136.         KillMenuItem(Window->MyMenuItem);
  137.         CheckOutDyingWindow(Window->MyGenericWindow);
  138.         DisposeTextEdit(Window->Editor);
  139.         KillWindow(Window->ScreenID);
  140.         ReleasePtr((char*)Window);
  141.     }
  142.  
  143.  
  144. void                                    InteractionWindowDoIdle(InteractionWindowRec* Window,
  145.                                                 MyBoolean CheckCursorFlag, OrdType XLoc, OrdType YLoc,
  146.                                                 ModifierFlags Modifiers)
  147.     {
  148.         CheckPtrExistence(Window);
  149.         TextEditUpdateCursor(Window->Editor);
  150.         if (CheckCursorFlag)
  151.             {
  152.                 if (TextEditIBeamTest(Window->Editor,XLoc,YLoc))
  153.                     {
  154.                         SetIBeamCursor();
  155.                     }
  156.                  else
  157.                     {
  158.                         SetArrowCursor();
  159.                     }
  160.             }
  161.     }
  162.  
  163.  
  164. void                                    InteractionWindowBecomeActive(InteractionWindowRec* Window)
  165.     {
  166.         OrdType                        XSize;
  167.         OrdType                        YSize;
  168.  
  169.         CheckPtrExistence(Window);
  170.         EnableTextEditSelection(Window->Editor);
  171.         XSize = GetWindowWidth(Window->ScreenID);
  172.         YSize = GetWindowHeight(Window->ScreenID);
  173.         SetClipRect(Window->ScreenID,XSize - 15,YSize - 15,XSize,YSize);
  174.         DrawBitmap(Window->ScreenID,XSize-15,YSize-15,GetGrowIcon(True/*enablegrowicon*/));
  175.     }
  176.  
  177.  
  178. void                                    InteractionWindowBecomeInactive(InteractionWindowRec* Window)
  179.     {
  180.         OrdType                        XSize;
  181.         OrdType                        YSize;
  182.  
  183.         CheckPtrExistence(Window);
  184.         DisableTextEditSelection(Window->Editor);
  185.         XSize = GetWindowWidth(Window->ScreenID);
  186.         YSize = GetWindowHeight(Window->ScreenID);
  187.         SetClipRect(Window->ScreenID,XSize - 15,YSize - 15,XSize,YSize);
  188.         DrawBitmap(Window->ScreenID,XSize-15,YSize-15,GetGrowIcon(False/*disablegrowicon*/));
  189.     }
  190.  
  191.  
  192. void                                    InteractionWindowJustResized(InteractionWindowRec* Window)
  193.     {
  194.         OrdType                        XSize;
  195.         OrdType                        YSize;
  196.  
  197.         CheckPtrExistence(Window);
  198.         XSize = GetWindowWidth(Window->ScreenID);
  199.         YSize = GetWindowHeight(Window->ScreenID);
  200.         SetClipRect(Window->ScreenID,0,0,XSize,YSize);
  201.         DrawBoxErase(Window->ScreenID,0,0,XSize,YSize);
  202.         SetTextEditPosition(Window->Editor,-1,-1,XSize + 2,YSize + 2);
  203.     }
  204.  
  205.  
  206. void                                    InteractionWindowDoMouseDown(OrdType XLoc, OrdType YLoc,
  207.                                                 ModifierFlags Modifiers, InteractionWindowRec* Window)
  208.     {
  209.         CheckPtrExistence(Window);
  210.         if ((XLoc >= GetWindowWidth(Window->ScreenID) - 15)
  211.             && (XLoc < GetWindowWidth(Window->ScreenID))
  212.             && (YLoc >= GetWindowHeight(Window->ScreenID) - 15)
  213.             && (YLoc < GetWindowHeight(Window->ScreenID)))
  214.             {
  215.                 UserGrowWindow(Window->ScreenID,XLoc,YLoc);
  216.                 InteractionWindowJustResized(Window);
  217.             }
  218.         else if (TextEditHitTest(Window->Editor,XLoc,YLoc))
  219.             {
  220.                 TextEditDoMouseDown(Window->Editor,XLoc,YLoc,Modifiers);
  221.             }
  222.     }
  223.  
  224.  
  225. void                                    InteractionWindowDoKeyDown(unsigned char KeyCode,
  226.                                                 ModifierFlags Modifiers, InteractionWindowRec* Window)
  227.     {
  228.         CheckPtrExistence(Window);
  229.         TextEditDoKeyPressed(Window->Editor,KeyCode,Modifiers);
  230.     }
  231.  
  232.  
  233. void                                    InteractionWindowClose(InteractionWindowRec* Window)
  234.     {
  235.         CheckPtrExistence(Window);
  236.         DisposeInteractionWindow(Window);
  237.     }
  238.  
  239.  
  240. void                                    InteractionWindowUpdator(InteractionWindowRec* Window)
  241.     {
  242.         OrdType            XSize;
  243.         OrdType            YSize;
  244.  
  245.         CheckPtrExistence(Window);
  246.         TextEditFullRedraw(Window->Editor);
  247.         XSize = GetWindowWidth(Window->ScreenID);
  248.         YSize = GetWindowHeight(Window->ScreenID);
  249.         SetClipRect(Window->ScreenID,XSize - 15,YSize - 15,XSize,YSize);
  250.         DrawBitmap(Window->ScreenID,XSize-15,YSize-15,
  251.             GetGrowIcon(Window->MyGenericWindow == GetCurrentWindowID()));
  252.     }
  253.  
  254.  
  255. void                                    InteractionWindowMenuSetup(InteractionWindowRec* Window)
  256.     {
  257.         CheckPtrExistence(Window);
  258.         MainWindowEnableGlobalMenus(Window->MainWindow);
  259.         EnableMenuItem(mPaste);
  260.         ChangeItemName(mPaste,"Paste Text");
  261.         if (TextEditIsThereValidSelection(Window->Editor))
  262.             {
  263.                 EnableMenuItem(mCut);
  264.                 ChangeItemName(mCut,"Cut Text");
  265.                 EnableMenuItem(mCopy);
  266.                 ChangeItemName(mCopy,"Copy Text");
  267.                 EnableMenuItem(mClear);
  268.                 ChangeItemName(mClear,"Clear Text");
  269.             }
  270.         EnableMenuItem(mShiftLeft);
  271.         EnableMenuItem(mShiftRight);
  272.         EnableMenuItem(mBalanceParens);
  273.         EnableMenuItem(mSelectAll);
  274.         ChangeItemName(mSelectAll,"Select All Text");
  275.         if (TextEditCanWeUndo(Window->Editor))
  276.             {
  277.                 EnableMenuItem(mUndo);
  278.                 ChangeItemName(mUndo,"Undo Text Change");
  279.             }
  280.         ChangeItemName(mCloseFile,"Close Interaction");
  281.         EnableMenuItem(mCloseFile);
  282.         EnableMenuItem(mFind);
  283.         if (PtrSize(GlobalSearchString) != 0)
  284.             {
  285.                 EnableMenuItem(mFindAgain);
  286.                 if (TextEditIsThereValidSelection(Window->Editor))
  287.                     {
  288.                         EnableMenuItem(mReplace);
  289.                         EnableMenuItem(mReplaceAndFindAgain);
  290.                     }
  291.             }
  292.         EnableMenuItem(mShowSelection);
  293.         if (TextEditIsThereValidSelection(Window->Editor))
  294.             {
  295.                 EnableMenuItem(mEnterSelection);
  296.             }
  297.         SetItemCheckmark(Window->MyMenuItem);
  298.     }
  299.  
  300.  
  301. void                                    InteractionWindowDoMenuCommand(InteractionWindowRec* Window,
  302.                                                 MenuItemType* MenuItem)
  303.     {
  304.         CheckPtrExistence(Window);
  305.         if (MainWindowDoGlobalMenuItem(Window->MainWindow,MenuItem))
  306.             {
  307.             }
  308.         else if (MenuItem == mPaste)
  309.             {
  310.                 TextEditDoMenuPaste(Window->Editor);
  311.             }
  312.         else if (MenuItem == mCut)
  313.             {
  314.                 TextEditDoMenuCut(Window->Editor);
  315.             }
  316.         else if (MenuItem == mCopy)
  317.             {
  318.                 TextEditDoMenuCopy(Window->Editor);
  319.             }
  320.         else if (MenuItem == mClear)
  321.             {
  322.                 TextEditDoMenuClear(Window->Editor);
  323.             }
  324.         else if (MenuItem == mSelectAll)
  325.             {
  326.                 TextEditDoMenuSelectAll(Window->Editor);
  327.             }
  328.         else if (MenuItem == mUndo)
  329.             {
  330.                 TextEditDoMenuUndo(Window->Editor);
  331.             }
  332.         else if (MenuItem == mCloseFile)
  333.             {
  334.                 InteractionWindowClose(Window);
  335.             }
  336.         else if (MenuItem == mShiftLeft)
  337.             {
  338.                 TextEditShiftSelectionLeftOneTab(Window->Editor);
  339.             }
  340.         else if (MenuItem == mShiftRight)
  341.             {
  342.                 TextEditShiftSelectionRightOneTab(Window->Editor);
  343.             }
  344.         else if (MenuItem == mBalanceParens)
  345.             {
  346.                 TextEditBalanceParens(Window->Editor);
  347.             }
  348.         else if (MenuItem == mFind)
  349.             {
  350.                 switch (DoFindDialog(&GlobalSearchString,&GlobalReplaceString,
  351.                     mCut,mPaste,mCopy,mUndo,mSelectAll,mClear))
  352.                     {
  353.                         default:
  354.                             EXECUTE(PRERR(ForceAbort,
  355.                                 "InteractionWindowDoMenuCommand:  bad value from DoFindDialog"));
  356.                             break;
  357.                         case eFindCancel:
  358.                         case eDontFind:
  359.                             break;
  360.                         case eFindFromStart:
  361.                             SetTextEditInsertionPoint(Window->Editor,0,0);
  362.                             TextEditFindAgain(Window->Editor,GlobalSearchString);
  363.                             TextEditShowSelection(Window->Editor);
  364.                             break;
  365.                         case eFindAgain:
  366.                             TextEditFindAgain(Window->Editor,GlobalSearchString);
  367.                             TextEditShowSelection(Window->Editor);
  368.                             break;
  369.                     }
  370.             }
  371.         else if (MenuItem == mFindAgain)
  372.             {
  373.                 TextEditFindAgain(Window->Editor,GlobalSearchString);
  374.                 TextEditShowSelection(Window->Editor);
  375.             }
  376.         else if (MenuItem == mReplace)
  377.             {
  378.                 if (TextEditIsThereValidSelection(Window->Editor))
  379.                     {
  380.                         TextEditInsertRawDataWithUndo(Window->Editor,GlobalReplaceString,
  381.                             SYSTEMLINEFEED);
  382.                     }
  383.             }
  384.         else if (MenuItem == mReplaceAndFindAgain)
  385.             {
  386.                 if (TextEditIsThereValidSelection(Window->Editor))
  387.                     {
  388.                         TextEditInsertRawDataWithUndo(Window->Editor,GlobalReplaceString,
  389.                             SYSTEMLINEFEED);
  390.                         TextEditFindAgain(Window->Editor,GlobalSearchString);
  391.                         TextEditShowSelection(Window->Editor);
  392.                     }
  393.             }
  394.         else if (MenuItem == mShowSelection)
  395.             {
  396.                 TextEditShowSelection(Window->Editor);
  397.             }
  398.         else if (MenuItem == mEnterSelection)
  399.             {
  400.                 char*                        NewString;
  401.  
  402.                 NewString = TextEditGetSelection(Window->Editor);
  403.                 if (NewString != NIL)
  404.                     {
  405.                         ReleasePtr(GlobalSearchString);
  406.                         GlobalSearchString = NewString;
  407.                     }
  408.             }
  409.         else
  410.             {
  411.                 EXECUTE(PRERR(AllowResume,"InteractionWindowDoMenuCommand:  unknown menu command"));
  412.             }
  413.     }
  414.  
  415.  
  416. /* add a string to the interaction window.  linefeeds correspond to system linefeed */
  417. /* strings.  the string is a non-null-terminated block of data with explicit size. */
  418. MyBoolean                            InteractionWindowAppendString(InteractionWindowRec* Window,
  419.                                                 char* Data, long Length)
  420.     {
  421.         char*                                Block;
  422.         MyBoolean                        Success;
  423.  
  424.         CheckPtrExistence(Window);
  425.         Block = GetTextEditLine(Window->Editor,GetTextEditNumLines(Window->Editor) - 1);
  426.         if (Block == NIL)
  427.             {
  428.                 return False;
  429.             }
  430.         SetTextEditInsertionPoint(Window->Editor,GetTextEditNumLines(Window->Editor) - 1,
  431.             PtrSize(Block));
  432.         ReleasePtr(Block);
  433.         Block = BlockFromRaw(Data,Length);
  434.         if (Block == NIL)
  435.             {
  436.                 return False;
  437.             }
  438.         Success = TextEditInsertRawData(Window->Editor,Block,SYSTEMLINEFEED);
  439.         ReleasePtr(Block);
  440.         TextEditShowSelection(Window->Editor);
  441.         return Success;
  442.     }
  443.